<?php
namespace App\Services\Main;

use App\Repositories\Admin\ComStaffsRepository;
use App\Repositories\Admin\UserRepository;
use App\Repositories\Main\MsgGroupDetailRepository;
use App\Repositories\Main\MsgGroupRepository;

class MsgGroupService
{
    private $service;
    private $comStaff;
    private $detail;
    private $user;

    public function __construct()
    {
        $this->service = new MsgGroupRepository();
        $this->comStaff = new ComStaffsRepository();
        $this->detail = new MsgGroupDetailRepository();
        $this->user = new UserRepository();
    }

    public function getIndexByUid($uid)
    {
        $cid = $this->comStaff->getCidByUid($uid);
        $models = $this->service->getGroupsByCid($cid);
        if (!count($models)) { return ''; }
        $dataArr = array();
        foreach ($models as $k=>$model) {
            $dataArr[$k]['id'] = $model->id;
            $dataArr[$k]['name'] = $model->name;
            $dataArr[$k]['lastTime'] = date('Y-m-d',$model->updated_at);
        }
        return $dataArr;
    }

    public function getDetails($uid,$group)
    {
        $models = $this->detail->getIndexByGroup($uid,$group);
        if (!count($models)) { return ''; }
        $dataArr = array();
        foreach ($models as $k=>$model) {
            $user = $this->user->getShowOne($model->uid);
            $dataArr[$k]['id'] = $model->id;
            $dataArr[$k]['align'] = $model->uid==$uid?'right':'left';
            $dataArr[$k]['head'] = $user ? $user->head : '';
            $dataArr[$k]['detail'] = $model->detail;
        }
        return $dataArr;
    }

    public function setSend($group,$uid,$detail)
    {
        $dataArr = array(
            'group' => $group,
            'uid' => $uid,
            'detail' => $detail,
        );
        return $this->detail->setAdd($dataArr);
    }

    public function getStaffs($group)
    {
        $models = $this->comStaff->getShowByGroup($group);
        if (!count($models)) { return ''; }
        $dataArr = array();
        foreach ($models as $k=>$model) {
            $user = $this->user->getShowOne($model->uid);
            $dataArr[$k]['uid'] = $model->uid;
            $dataArr[$k]['name'] = $user ? $user->name : '测试';
        }
        return $dataArr;
    }

//    public function add($data)
//    {
//        return $this->service->setAdd($data);
//    }

//    public function getOne($id)
//    {
//        return $this->service->getShowOne($id);
//    }
}